﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using Acms.Bll;

namespace Acms.AcmsControls
{
    public class RelatedPostsContentControlProperties : BaseContentControlProperties
    {
        private List<Guid> _PostGuids = new List<Guid>();
        public Guid[] PostGuids
        {
            get
            {
                return _PostGuids.ToArray();
            }
        }

        public void AddPostGuid(Guid postGuid)
        {
            if (!_PostGuids.Contains(postGuid))
            {
                _PostGuids.Add(postGuid);
            }
        }

        public void AddPostGuid(Post post)
        {
            if (post != null)
            {
                AddPostGuid(post.PostGuid);
            }
        }

        public void SetPostGuids(Guid[] postGuids)
        {
            _PostGuids.Clear();
            _PostGuids.AddRange(postGuids);
        }

        public void AddRange(Guid[] postGuids)
        {
            foreach (Guid id in postGuids)
            {
                AddPostGuid(id);
            }
        }

        public void Remove(Guid id)
        {
            if (_PostGuids.Contains(id))
            {
                _PostGuids.Remove(id);
            }
        }

        public void Move(int movingIndex, int replacingIndex)
        {
            if (movingIndex < _PostGuids.Count && replacingIndex < _PostGuids.Count && movingIndex >=0 && replacingIndex >= 0 && movingIndex != replacingIndex)
            {
                Guid tmp = _PostGuids[replacingIndex];
                _PostGuids[replacingIndex] = _PostGuids[movingIndex];
                _PostGuids[movingIndex] = tmp;
            }
        }

        public Post[] GetPosts(int siteId, DateTime? siteVersion)
        {
            List<Post> tmp = new List<Post>();
            foreach (Guid id in _PostGuids)
            {
                var post = Post.GetByPostGuid(siteId, id, siteVersion);
                if(post != null)
                    tmp.Add(post);
            }
            return tmp.ToArray();
        }

        public bool Contains(Guid id)
        {
            return _PostGuids.Contains(id);
        }

        public override void ReadXml(System.Xml.XmlReader reader)
        {
            _PostGuids.Clear();
            base.ReadXml(reader);
        }

        protected override void ReadValue(System.Xml.XmlReader reader)
        {
            if (reader.Name.ToLower() == "guid" && (reader.NodeType == System.Xml.XmlNodeType.Element || reader.NodeType == System.Xml.XmlNodeType.Attribute))
            {
                if (reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    reader.Read();
                }
                try
                {
                    AddPostGuid(new Guid(reader.ReadContentAsString()));
                }
                catch
                {
                }

            }
        }

        protected override void  WriterPropertiesToXml(XmlWriter writer)
        {
            writer.WriteStartElement("content");

            foreach (Guid id in _PostGuids)
            {
                writer.WriteStartElement("post");
                writer.WriteAttributeString("guid", id.ToString());
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
    }

}
